package com.egoo.ticket.server.utils.common;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

/**
 * @author：victor_tang
 * @Date: 2019/10/7
 * @Description: Http请求工具类
 */
@Slf4j
public class HttpRequestUtil {

	/**
	 * TODO 发送post请求
	 * @author: victor_tang
	 * @createtime: 2020/2/26 12:33
	 * @param requestUrl 1
	 * @param requestBodyParams 2
	 * @param requestHeaderParamMap 3
	 * @return: java.lang.String
	 */
	public static String sendPost(String requestUrl, String requestBodyParams,
			Map<String, Object> requestHeaderParamMap) throws IOException {

		// 客户端实例化
		HttpClient httpClient = new HttpClient();
		PostMethod postMethod = new PostMethod(requestUrl);
		// 设置请求头参数
		if (requestHeaderParamMap != null) {
			Set<String> requestHeaderParamKeyset = requestHeaderParamMap.keySet();
			for (String key : requestHeaderParamKeyset) {
				postMethod.setRequestHeader(key, requestHeaderParamMap.get(key) + "");
			}
		}
		postMethod.setRequestHeader("Content-Type", "application/json");
		postMethod.setRequestHeader("charset", "utf-8");

		//设置请求体,将参数转为二进制流
		byte[] requestBytes = requestBodyParams.getBytes("utf-8");
		InputStream inputStream = new ByteArrayInputStream(requestBytes, 0, requestBytes.length);

		// 请求体
		RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, requestBytes.length,
				"application/json; charset=utf-8");
		postMethod.setRequestEntity(requestEntity);

		// 执行请求
		httpClient.executeMethod(postMethod);

		// 获取返回的流
		InputStream soapResponseStream = postMethod.getResponseBodyAsStream();
		byte[] datas = null;
		try {
			// 从输入流中读取数据
			datas = readInputStream(soapResponseStream);

		} catch (Exception e) {
			log.info("输入流中读取数据异常：" + e);
		}
		// 将二进制流转为String
		String result = new String(datas, "UTF-8");

		return result;
	}

	/**
	 * TODO 从输入流中读取数据
	 *
	 * @param inStream 1
	 * @author: victor_tang
	 * @createtime: 2019/10/7 15:27
	 * @return: byte[]
	 */
	public static byte[] readInputStream(InputStream inStream) throws Exception {
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outStream.write(buffer, 0, len);
		}
		byte[] data = outStream.toByteArray();
		outStream.close();
		inStream.close();
		return data;
	}
//	/**
//	 * TODO get请求方式1
//	 * @author: victor_tang
//	 * @createtime: 2020/2/26 12:35
//	 * @param strUrl 1
//	 * @return: java.lang.String
//	 */
//	public static String getURLContentByGet(String strUrl) throws Exception {
//		URL url = new URL(strUrl);
//		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
//		httpConn.setRequestMethod("GET");
//		httpConn.connect();
//
//		BufferedReader reader = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
//		String line;
//		StringBuffer buffer = new StringBuffer();
//		while ((line = reader.readLine()) != null) {
//			buffer.append(line);
//		}
//		reader.close();
//		httpConn.disconnect();
//		return buffer.toString();
//	}

	/**
	 * TODO get请求方式2
	 * @author: victor_tang
	 * @createtime: 2020/2/26 12:36
	 * @param urlStr 1
	 * @return: java.lang.String
	 */
	public static String getUrlContent(String urlStr) {
		/** 网络的url地址 */
		URL url = null;
		/** http连接 */
		HttpURLConnection httpConn = null;
		/**//** 输入流 */
		BufferedReader in = null;
		StringBuffer sb = new StringBuffer();
		try {
			url = new URL(urlStr);
			in = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"));
			String str = null;
			while ((str = in.readLine()) != null) {
				sb.append(str);
			}
		} catch (Exception ex) {

		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
			}
		}
		return sb.toString();
	}
}
